/**
 * 后台上传图片管理。
 *
 * @author    Fairyin <fairyin@126.com>
 * @copyright © 2016 www.imcn.vip
 * @license   GPL-3.0
 */

$(document).ready(function(){
    if (!$(".scopes_upload").length) return;

    if ("object" != typeof _upload) _upload = {};

    var uploader = WebUploader.create({
        // 选完文件后，是否自动上传。
        auto: false,
        // 文件接收服务端。
        server: '//imcn.vip/shadow/upload',
        // 选择文件的按钮。可选。
        // 内部根据当前运行是创建，可能是input元素，也可能是flash.
        pick: '#addimg',
        // 只允许选择图片文件。
        accept: {
            title: 'Images',
            extensions: 'gif,jpg,jpeg,bmp,png',
            mimeTypes: 'image/*'
        },
        formData: {
            
        },
        fileVal: 'img',
        compress: false,
        threads: 1
    });
    
    uploader.on('filesQueued', function(files) {
        for (var ii in files) {
            uploader.makeThumb(files[ii], function(error, ret) {
                if (error) {
                    console.log('预览错误');
                } else {
                    var ll = $(".duilie img").length + 1;
                    var str = "<img src='" + ret + "' data-key='" + ll + "'>";
                    $(".duilie").append(str);
                }
            });
        }
    });
    
    _upload.duilie = parseInt($(".scopes_upload input[name='start']").val());
    
    uploader.on('uploadStart', function(file){
        uploader.options.formData.key = 'dtl/' + $(".btn-save").attr('data-id') + '/' + _upload.duilie + '.jpg';
        uploader.options.formData._token  = $("#upload").attr('data-token');
    });
    
    uploader.on('uploadSuccess', function(file, response){
        if (typeof(response.key) !== 'undefined') {
            $(".scopes_upload input[name='end']").val(_upload.duilie);
            if (typeof(_upload.items) === 'undefined') {
                _upload.items = {};
            }
            _upload.items[_upload.duilie] = response.ext;
            _upload.duilie += 1;
        }
    });
    
    $("#upload").on('click', function(){
        uploader.upload();
    });
    
    _upload.save = function(){
        var url = '//imcn.vip/shadow/photo/detail/' + $(this).attr('data-id');
        var token = $("#upload").attr('data-token');
        common.ajax(url, 'POST', {
            qianzhui: $(".scopes_upload input[name='qianzhui']").val(),
            houzhui: $(".scopes_upload input[name='houzhui']").val(),
            start: $(".scopes_upload input[name='start']").val(),
            end: $(".scopes_upload input[name='end']").val(),
            type: JSON.stringify(_upload.items)
        }, function(data){
            console.log(data);
        }, function(error){
            console.log(error);
        }, token);
    };
    
    $(".scopes_upload .btn-save").on('click', _upload.save);
    
    $(".scopes_upload input[name='start']").on('keyup', function(){
        var duilie  = parseInt($(this).val());
        if (isNaN(duilie)) {
            return false;
        }
        _upload.duilie = duilie;
    });
    
    
});
